﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using M7;
using System.Linq;

namespace C7
{
	/// <summary>
	/// Interaction logic for InternetModule.xaml
	/// </summary>
    public partial class ChatModule : UserControl
    {
        private WindowOperations WindowOPs = new WindowOperations();
        
        public ChatModule()
        {
            this.InitializeComponent();
            WindowOPs.TextBoxesToHandlers(LayoutRoot);
            WindowOPs.WindowToAnimationHandlers(LayoutRoot, true);
        }

        private void ChatM_Loaded(object sender, RoutedEventArgs e)
        {
            if (Info.current.customers.Count > 0)
            {
                Thread thrCon = new Thread(delegate() { Chat.Connect(); });
                thrCon.IsBackground = true;
                thrCon.Start();
            }
        }

        private void SendMsg(string text)
        {
            if (text != "")
            {
                Thread thrCon = new Thread(delegate() { Chat.SendMsg(text); });
                thrCon.IsBackground = true;
                thrCon.Start();

            }
        }
        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            string text = tbWrittenText.Text;
            tbWrittenText.Text = String.Empty;
            Thread thr = new Thread(delegate() { SendMsg(text); });
            thr.IsBackground = true;
            thr.Start();
            tbWrittenText.Focus();
        }
        private void tbWrittenText_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                string text = tbWrittenText.Text;
                tbWrittenText.Text = String.Empty;
                Thread thr = new Thread(delegate() { SendMsg(text); });
                thr.IsBackground = true;
                thr.Start();
            }
        }   
    }
}